package top.january.codelabf2;

import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.window.service.WindowManager;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends AceAbility {

    @Override
    public void onStart(Intent intent) {
        // 将internalAbility绑定提前到super.onStart调用前（因为在分布式迁移时发现
        // app.js的onCreate方法会在internalAbility绑定前调用）
        InMainAbility.getInstance().bind(this);
        super.onStart(intent);
        requestPermission();
        MyApplication app = (MyApplication)getAbilityPackage();
        app.loadData();

    }

    @Override
    public void onStop() {
        super.onStop();
        InMainAbility.getInstance().unbind();
        MyApplication app = (MyApplication)getAbilityPackage();
        app.saveData();
    }

    private void requestPermission() {
        String[] permissions = {
                "ohos.permission.DISTRIBUTED_DATASYNC"
        };
        List<String> applyPermissions = new ArrayList<>();
        for (String element : permissions) {
            if (verifySelfPermission(element) != 0) {
                if (canRequestPermission(element)) {
                    applyPermissions.add(element);
                }
            }
        }
        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);
    }

    public void fullScreen() {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
    }

    public void exitFullScreen() {
        getWindow().clearFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
    }
}
